//
//  RTVisitorView.m
//  WeiboSina
//
//  Created by Brisinga on 16/3/4.
//  Copyright © 2016年 Root. All rights reserved.
//

#import "RTVisitorView.h"

@interface RTVisitorView()

@property (nonatomic, strong) UIImageView *iconImage;

@property (nonatomic, strong) UIImageView *circleImage;

@property (nonatomic, strong) UIImageView *maskIconView;

@property (nonatomic, strong) UILabel *messageLabel;

@property (nonatomic, strong) UIButton *loginBtn;

@property (nonatomic, strong) UIButton *registerBtn;

@end

@implementation RTVisitorView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}


#pragma mark - 加载内部控件
- (void)setupUI {
    
    self.backgroundColor = [UIColor colorWithWhite:237/255.0 alpha:1];
    
    
    [self addSubview:self.circleImage];
    [self addSubview:self.maskIconView];
    [self addSubview:self.iconImage];
    [self addSubview:self.messageLabel];
    [self addSubview:self.loginBtn];
    [self addSubview:self.registerBtn];
    
    // 布局子控件
    [self setLayout];
    
}

#pragma mark - 设置visitor的信息
- (void)setVisitorViewWithImageName:(NSString *)imageName message:(NSString *)message {
    
    if (imageName) {
        
        self.iconImage.image = [UIImage imageNamed:imageName];
        
        self.messageLabel.text = message;
        
        self.circleImage.hidden = YES;
        
    }else {
        
//        [self setupUI];
        [self startAnimation];
    }
    
    
}



#pragma mark - 开启circle动画
- (void)startAnimation {
    
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation"];
    
    animation.toValue = @(2 * M_PI);
    
    animation.repeatCount = MAXFLOAT;
    
    animation.duration = 20;
    
    animation.removedOnCompletion = NO;
    
    [self.circleImage.layer addAnimation:animation forKey:nil];

}


#pragma mark - 登录按钮点击事件
- (void)loginBtnDidClick:(UIButton *)sender {

    if ([self.delegate respondsToSelector:@selector(visitorView:didClickLogin:)]) {
        
        [self.delegate visitorView:self didClickLogin:sender];
    }
}



#pragma mark - 注册按钮点击事件
- (void)registerBtnDidClick:(UIButton *)sender {

    if ([self.delegate respondsToSelector:@selector(visitorView:didClickRegister:)]) {
        
        [self.delegate visitorView:self didClickRegister:sender];
    }
}



#pragma mark - 布局子控件 
- (void)setLayout {
    
    // 布局iconImage
    [self.iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.centerY.offset(0);
        
    }];
    
    // 布局circleImage
    [self.circleImage mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.centerY.offset(0);
        
    }];
    
    // 布局遮罩
    [self.maskIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.offset(0);
        make.centerY.equalTo(self.circleImage.mas_centerY).offset(-8);
    }];
    
    // 布局messageLabel
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.maskIconView.mas_bottom).offset(15);
        make.centerX.offset(0);
        make.width.mas_equalTo(225);
    }];
    
    // 布局注册按钮
    [self.registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.messageLabel);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(100);
    }];
    
    // 布局登录按钮
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(self.messageLabel);
        make.width.top.equalTo(self.registerBtn);
    }];
}


#pragma mark - 懒加载

//iconImage
- (UIImageView *)iconImage {
    
    if (nil==_iconImage) {
        
        _iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_house"]];
    }
    
    return _iconImage;
}

//circleImage
- (UIImageView *)circleImage {
    
    if (nil==_circleImage) {
        
        _circleImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_image_smallicon"]];
    }
    
    return _circleImage;
}


//messageLabel
- (UILabel *)messageLabel {

    if (nil==_messageLabel) {
        
        _messageLabel = [UILabel labelWithTitle:@"关注一些人，回这里看看有什么惊喜" textColor:[UIColor darkGrayColor] textFont:14];
        
        _messageLabel.numberOfLines = 0;
        
        _messageLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _messageLabel;
}


//loginBtn
- (UIButton *)loginBtn {
    
    if (nil==_loginBtn) {
        
        _loginBtn = [UIButton buttonWithTitle:@"登录" textColor:[UIColor darkGrayColor] textFont:14];
        
        [_loginBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
        
        [_loginBtn addTarget:self action:@selector(loginBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}


//registerBtn
- (UIButton *)registerBtn {

    if (nil==_registerBtn) {
        
        _registerBtn = [UIButton buttonWithTitle:@"注册" textColor:[UIColor orangeColor] textFont:14];
        
        [_registerBtn setBackgroundImage:[UIImage imageNamed:@"common_button_white"] forState:UIControlStateNormal];
        
        [_registerBtn addTarget:self action:@selector(registerBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

//maskIconView
- (UIImageView *)maskIconView {
    
    if (nil==_maskIconView) {
        
        _maskIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"visitordiscover_feed_mask_smallicon"]];
    }
    
    return _maskIconView;
}

@end
